﻿//***********************************************************************************************************************
//
//文件名(File Name):     IminectPlayerInfo .cs
//
//功能描述(Description):    玩家体感数据存储类
//
//作者(Author):		郭佳
//
//日期(Create Date):	
//
//修改记录(Revision History):
//			R1：
//				修改作者:
//				修改日期:
//				修改理由:				
//
//***********************************************************************************************************************

using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;

namespace IMIForUnity
{

    public class ImiPlayerInfo : IPlayerInfo
    {
        public int userId;
        public bool playerTracked;
        public Vector3 playerPosition;
        public Vector3 playerGround;
        public Vector3[] playerLimbsAngular;
        public ImiSkeleton[] skeletons;
        public Vector3[] playerJointsPos;
        public Vector3[] playerJointsSpeed;
        public bool[] playerJointsTracked;
        public Dictionary<System.Type, ImiGesture> playerGestures;
        public Dictionary<ImiGesturePro.GestureType, ImiGesture.OnGestureEvent> playerGesturesPro;
        public bool isOrigFixRec = false;

        public ImiPlayerInfo()
        {
            this.userId = 0;
            this.playerTracked = false;
            this.playerPosition = new Vector3();

            this.playerGestures = new Dictionary<System.Type, ImiGesture>();
            this.playerGesturesPro = new Dictionary<ImiGesturePro.GestureType, ImiGesture.OnGestureEvent>();
            this.isOrigFixRec = false;
            this.playerLimbsAngular = new Vector3[(int)Limb.Count];
            for (int i = 0; i < (int)Limb.Count; i++)
            {
                playerLimbsAngular[i] = new Vector3(-1.0f, -1.0f, -1.0f);
            }
            skeletons = new ImiSkeleton[(int)ImiSkeleton.Index.COUNT];
            playerJointsPos = new Vector3[(int)ImiSkeleton.Index.COUNT];
            playerJointsSpeed = new Vector3[(int)ImiSkeleton.Index.COUNT];
            playerJointsTracked = new bool[(int)ImiSkeleton.Index.COUNT];

            Dictionary<System.Type, ImiGesture.OnGestureEvent> gestures = ImiGestureManager.GetInstance().GetOtherPlayerGestures();
            foreach(System.Type g in gestures.Keys)
            {
                ImiGesture gesture = (ImiGesture)Activator.CreateInstance(g);
                gesture.GestureEventListeners += gestures[g];
            }

            Dictionary<ImiGesturePro.GestureType, ImiGesture.OnGestureEvent> gesturesPro = ImiGestureManager.GetInstance().GetOtherPlayerGesturesPro();
            foreach(ImiGesturePro.GestureType g in gesturesPro.Keys)
            {
                playerGesturesPro.Add(g, new ImiGesture.OnGestureEvent(gesturesPro[g]));
            }
        }

        public ImiSkeleton[] GetSkeletons()
        {
            return this.skeletons;
        }

        public bool[] GetPlayerJointsTracked()
        {
            //TODO this method needs to cache the result to avoid recaculate everytime
            for (int i = 0; i < skeletons.Length; i++)
            {
                playerJointsTracked[i] = skeletons[i].isTracked;
            }
            return playerJointsTracked;
        }

        public Vector3[] GetPlayerJointsPos()
        {
            //TODO this method needs to cache the result to avoid recaculate everytime
            for (int i = 0; i < skeletons.Length; i++)
            {
                playerJointsPos[i] = skeletons[i].position;
            }
            return playerJointsPos;
        }

        public Vector3[] GetPlayerJointsSpeed()
        {
            //TODO this method needs to cache the result to avoid recaculate everytime
            for (int i = 0; i < skeletons.Length; i++)
            {
                playerJointsSpeed[i] = skeletons[i].speed;
            }
            return playerJointsSpeed;
        }

        internal void Update(ImiWrapper.ImiSkeletonFrame skeletonFrame)
        {
            //TODO
        }

        internal void SetUserId(int id)
        {
            this.userId = id;
        }

        internal void SetPlayerLimbsAngular(int limb, Vector3 angular)
        {
            this.playerLimbsAngular[limb] = angular;
        }

        public Vector3[] GetPlayerLimbsAngular()
        {
            return this.playerLimbsAngular;
        }


        internal void SetPlayerTracked(bool tracked)
        {
            this.playerTracked = tracked;
        }

        internal void SetPlayerPosition(float x, float y, float z)
        {
            this.playerPosition.x = x;
            this.playerPosition.y = y;
            this.playerPosition.z = z;
        }


        public int GetUserId()
        {
            return this.userId;
        }


        public bool GetPlayerTracked()
        {
            return this.playerTracked;
        }

        public Vector3 GetPlayerPosition()
        {
            return this.playerPosition;
        }

        internal void ClearPlayerGestures()
        {
            playerGestures.Clear();
        }

    }

}